package com.eorchis.module.basedata.service.impl;

import com.eorchis.core.annotation.BusinessLogForAdd;
import com.eorchis.core.annotation.BusinessLogForBatchUpdateState;
import com.eorchis.core.annotation.BusinessLogForUpdate;
import com.eorchis.core.annotation.CreateInfo;
import com.eorchis.core.basedao.condition.BaseCondition;
import com.eorchis.core.page.commond.BasePageCommond;
import com.eorchis.core.page.commond.JSONObject;
import com.eorchis.core.servicetemplate.pagetemplate.PageTemplate;
import com.eorchis.module.basedata.domain.BaseData;
import com.eorchis.module.basedata.domain.BaseDataCondition;
import com.eorchis.module.basedata.domain.BaseDataType;
import com.eorchis.module.basedata.domain.jsonbean.BaseDataJsonBean;
import com.eorchis.module.basedata.manager.IBaseDataManager;
import com.eorchis.module.basedata.service.IBaseDataService;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service("com.eorchis.module.basedata.service.impl.BaseDataServiceImpl")
public class BaseDataServiceImpl extends PageTemplate
  implements IBaseDataService
{

  @Autowired
  @Qualifier("com.eorchis.module.basedata.manager.impl.BaseDataManagerImpl")
  private IBaseDataManager baseDataManager;

  @BusinessLogForAdd(operationModule="基础数据")
  @CreateInfo(createTimeMethod="setCreateTime", creatorMethod="setCreator")
  public void addBaseData(BaseData baseData)
    throws Exception
  {
    this.baseDataManager.addBaseData(baseData);

    BaseDataUtil.update(baseData);
  }

  @BusinessLogForBatchUpdateState(entityClass=BaseData.class, getEntityPKMethodName="getDataID", getObjectsByIDArrayMethodName="getBaseDataListByIDArray", getPKArrayMethodName="getBaseDataIds", operationModule="基础数据", pkArrayClass=Long[].class)
  public void discardOrReuseBaseData(BaseDataCondition condition)
    throws Exception
  {
    this.baseDataManager.discardOrReuseBaseData(condition);

    if ((condition.getBaseDataIds() != null) && (condition.getBaseDataIds().length > 0)) {
      String[] idArray = condition.getBaseDataIds();

      BaseDataUtil.discardOrReuseBaseData(getBaseDataListByIDArray(idArray));
    }
  }

  public BaseData getBaseData(BaseDataCondition condition)
    throws Exception
  {
    return this.baseDataManager.getBaseData(condition);
  }

  public List<BaseData> getBaseDataList(BaseDataCondition condition)
    throws Exception
  {
    return this.baseDataManager.getBaseDataList(condition);
  }

  @BusinessLogForUpdate(getEntityPKMethodName="getDataID", getObjectByIDMethodName="getBaseDataByID", operationModule="基础数据", pkClass=Long.class)
  @CreateInfo(createTimeMethod="setCreateTime", creatorMethod="setCreator")
  public void updateBaseData(BaseData baseData)
    throws Exception
  {
    BaseData oldData = this.baseDataManager.getBaseDataByID(baseData.getDataID());
    this.baseDataManager.updateBaseData(baseData);

    if (BaseData.IS_ACTIVE_Y.equals(oldData.getActiveState())) {
      BaseDataUtil.delete(new String[] { oldData.getDataCode() });
    }

    BaseDataUtil.update(baseData);
  }

  public BaseData viewBaseData(BaseDataCondition condition)
    throws Exception
  {
    return this.baseDataManager.viewBaseData(condition);
  }

  public JSONObject doProcess(BasePageCommond pageCommond, BaseCondition condition)
    throws Exception
  {
    return process(pageCommond, condition);
  }

  public boolean checkDataCodeIsRepeat(BaseData baseData)
    throws Exception
  {
    return this.baseDataManager.checkDataCodeIsRepeat(baseData);
  }

  protected Long findCount(BaseCondition condition)
    throws Exception
  {
    return this.baseDataManager.listBaseDataCount((BaseDataCondition)condition);
  }

  protected List<BaseData> findList(BaseCondition condition)
    throws Exception
  {
    return this.baseDataManager.listBaseData((BaseDataCondition)condition);
  }

  public BaseData getBaseDataByID(String id)
    throws Exception
  {
    return this.baseDataManager.getBaseDataByID(id);
  }

  public List<BaseData> getBaseDataListByIDArray(String[] idArray)
    throws Exception
  {
    return this.baseDataManager.getBaseDataListByIDArray(idArray);
  }

  protected List<?> objListToJsonList(List<?> pageResultList) throws Exception
  {
    List result = new ArrayList();
    for (Iterator i$ = pageResultList.iterator(); i$.hasNext(); ) { Object object = i$.next();
      BaseData baseData = (BaseData)object;
      BaseDataJsonBean bean = new BaseDataJsonBean();
      BeanUtils.copyProperties(baseData, bean);
      bean.setBaseDataTypeID(baseData.getBaseDataType().getTypeID());
      bean.setBaseDataTypeName(baseData.getBaseDataType().getTypeName());

      result.add(bean);
    }
    return result;
  }
}